﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace CPPEI.WebApi
{
    /// <summary>
    /// 集成
    /// </summary>
    public static class Intergration
    {
        public static class Config
        {
            public static IAuthProvider AuthProvider { get; private set; }

            /// <summary>
            /// 首字母是否小写,是否采用驼峰式命名法
            /// </summary>
            public static bool IsCamelCasePropertyName { get; private set; }

            /// <summary>
            /// 初始化配置
            /// </summary>
            public static void Init()
            {
                string configFile = System.Web.HttpContext.Current.Server.MapPath("~/Configs/WebApi.config");

                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(configFile);
                XmlNode xAuthMode = xDoc.SelectSingleNode("./WebApi/Authentication");
                if (xAuthMode != null)
                {
                    string mode = xAuthMode.Attributes["Mode"].Value.Trim();
                    if (string.Equals(mode, "Cookie"))
                    {
                        AuthProvider = new CookieAuthProvider();
                    }
                    else
                    {
                        AuthProvider = new FormsAuthProvider();
                    }
                }
                else
                {
                    AuthProvider = new FormsAuthProvider();
                }

                XmlNode xSettings = xDoc.SelectSingleNode("./WebApi/Settings");
                if (xSettings != null)
                {
                    foreach (XmlNode xSetting in xSettings.ChildNodes)
                    {
                        if (string.Equals(xSetting.Name, "CamelCasePropertyName", StringComparison.CurrentCultureIgnoreCase))
                        {
                            bool isTrue = true;
                            if (bool.TryParse(xSetting.InnerText, out isTrue))
                            {
                                IsCamelCasePropertyName = isTrue;
                            }
                            else
                            {
                                IsCamelCasePropertyName = isTrue;
                            }
                        }
                    }
                }
                else
                {
                    IsCamelCasePropertyName = true;
                }

                XmlNodeList xInitiators = xDoc.SelectNodes("./WebApi/Initiators/Initiator");
                for (int index = 0; index < xInitiators.Count; index++)
                {
                    XmlNode xInitiator = xInitiators[index];
                    string assemblyName = xInitiator.Attributes["Assembly"].Value.Trim();
                    string typeName = xInitiator.Attributes["Type"].Value.Trim();
                    IInitiator oInitiator = System.Reflection.Assembly.Load(assemblyName).CreateInstance(typeName) as IInitiator;
                    if (oInitiator != null)
                    {
                        try
                        {
                            oInitiator.Init();
                        }
                        catch(Exception ex)
                        {
                            Logger.Error(Library.LogTAG, string.Format("init initiator:{0} error, ex:{1}", typeName, ex));
                        }
                    }
                }

                //XmlNodeList xModules = xDoc.SelectNodes("./WebApi/Modules/Module");
                //for (int index = 0; index < xModules.Count; index++)
                //{
                //    XmlNode xModule = xModules[index];
                //    string assemblyName = xModule.Attributes["Assembly"].Value.Trim();
                //    string typeName = xModule.Attributes["Type"].Value.Trim();
                //}
            }
        }

    }
}
